define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数
            Table.api.init({
                extend: {
                    index_url: 'scanwork/factory/index' + location.search,
                    add_url: 'scanwork/factory/add',
                    edit_url: 'scanwork/factory/edit',
                    del_url: 'scanwork/factory/del',
                    multi_url: 'scanwork/factory/multi',
                    import_url: 'scanwork/factory/import',
                    table: 'scanwork_factory',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'sort',
                sortOrder: 'asc',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate: false},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'code', title: '工厂编码', operate: 'LIKE'},
                        {field: 'address', title: '工厂地址', operate: 'LIKE'},
                        {field: 'contact_person', title: '联系人', operate: 'LIKE'},
                        {field: 'contact_phone', title: '联系电话', operate: 'LIKE'},
                        {field: 'sort', title: '排序', operate: false},
                        {field: 'status', title: __('Status'), searchList: {"1":__('Normal'),"0":__('Hidden')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            // 用户管理按钮事件
            $(document).on('click', '.btn-users', function () {
                var ids = Table.api.selectedids(table);
                if (ids.length === 0) {
                    Toastr.warning("请选择要管理的工厂");
                    return false;
                }
                if (ids.length > 1) {
                    Toastr.warning("请选择一个工厂进行用户管理");
                    return false;
                }
                Fast.api.open('scanwork/factory/users/ids/' + ids[0], '工厂用户管理', {
                    area: ['90%', '90%']
                });
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        users: function () {
            // 初始化用户管理表格
            Table.api.init({
                extend: {
                    index_url: 'scanwork/factory/users' + location.search,
                    del_url: 'scanwork/factory/removeUser',
                    table: 'scanwork_user_factory',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                sortOrder: 'desc',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate: false},
                        {field: 'user.nickname', title: '用户名称', operate: false},
                        {field: 'user.username', title: '用户名', operate: false},
                        {field: 'role', title: '角色', searchList: {"admin":"管理员","manager":"主管","employee":"员工"}, formatter: function(value, row, index) {
                            var roleList = {"admin":"管理员","manager":"主管","employee":"员工"};
                            return roleList[value] || value;
                        }},
                        {field: 'status', title: __('Status'), searchList: {"1":__('Normal'),"0":__('Hidden')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            // 添加用户按钮事件
            $(document).on('click', '.btn-add-user', function () {
                Fast.api.open('scanwork/factory/addUser?factory_id=' + getUrlParam('ids'), '添加用户', {
                    area: ['50%', '70%'],
                    callback: function(data) {
                        $(".btn-refresh").trigger("click");
                    }
                });
            });
        },
        addUser: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
